﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Char_items
    {
        string[,] _char_items;
        private MySqlConnection conn;

        public void refresh()
        {
            string[] columns = new string[1];
            columns[0] = "*";

            this._char_items = Query.SelectQuery(this.conn, "char_items", columns);
        }

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public string[,] getItemsFromOwner(int owner, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[,] items = new string[0, this._char_items.GetLength(1)];

            for (int i = 0; i < this._char_items.GetLength(0); i++)
            {
                if (this._char_items[i, 1] == owner.ToString())
                {
                    Other.ResizetwoDimensArray(ref items, this._char_items.GetLength(1), this._char_items.GetLength(0) + 1);
                    for (int i2 = 0; i2 < this._char_items.GetLength(1); i2++)
                    {
                        items[items.GetLength(0) - 1, i2] = this._char_items[i, i2];
                    }
                }
            }

            return items;
        }

        public string[,] AllItems
        {
            get { return this._char_items; }
        }

        public void updateSlot(int id, int slot)
        {
            Query.UpdateQuery(this.conn, "char_items", "itemnumber,slot", "item" + slot.ToString() + "," + id.ToString(), "id=" + id);
        }

        public void removeItem(int id)
        {
            Query.DeleteQuery(this.conn, "char_items", "id=" + id);
        }

        public void updateAmount(int id, int newAmount)
        {
            Query.UpdateQuery(this.conn, "char_items", "quantity", newAmount.ToString(), "id=" + id);
        }

        public string[] getItem(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            string[] item = new string[this._char_items.GetLength(1)];
            for (int i = 0; i < item.Length; i++)
            {
                item[i] = this._char_items[id - 1, i];
            }

            return item;
        }

        public void addBlue(int id, int bluecount, string blue, int blueamount)
        {
            Query.UpdateQuery(this.conn, "char_items", "BlueAmount", bluecount.ToString(), "id=" + id);
            Query.UpdateQuery(this.conn, "char_items", "blue" + bluecount.ToString() + ",blue" + bluecount.ToString() + "amount", bluecount + "," + blueamount.ToString(), "id=" + id);
        }

        public void deleteBlueAtIndex(int id, int bluetodelete)
        {
            List<List<string>> blues = new List<List<string>>();
            List<string> blue = new List<string>();
            int blueamount;
            string[] result = Query.SelectQuery(this.conn,"char_items","BlueAmount","id=" + id);
            Int32.TryParse(result[0], out blueamount);
            //string[,] blues = new string[blueamount, 2];

            for (int i = 1; i < blueamount; i++)
            {
                result = Query.SelectQuery(this.conn,"char_items","blue" + i +",blue" + i +"amount","id=" + id);
                blue.Add(result[0]);
                blue.Add(result[1]);
                blues.Add(blue);
                blue.Clear();
            }

            blues.RemoveAt(bluetodelete);

            string[] columns = { "blue" + (blues.Count + 1), "blue" + (blues.Count + 1) + "amount"};
            string[] values = { "0", "0" };

            Query.UpdateQuery(this.conn, "char_items", "BlueAmount", blues.Count.ToString(), "id=" + id);
            Query.UpdateQuery(this.conn, "char_items", columns, values, "id=" + id);

            for (int i = 1; i < blues.Count; i++)
            {
                Query.UpdateQuery(this.conn, "char_items", "blue" + i, blues[i][0], "id=" + id);
                Query.UpdateQuery(this.conn, "char_items", "blue" + i + "amount", blues[i][1], "id=" + id);
            }

        }
    }
}
